package com.demo.wcx.greendaodemo;

import android.content.Context;
import android.util.Log;

import com.greendao.bean.MessageBean;
import com.greendao.dao.DaoSession;
import com.greendao.dao.MessageBeanDao;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by Administrator on 2016-05-26.
 */
public class DBHelper {
    private static final String TAG = DBHelper.class.getSimpleName();
    private static DBHelper instance;
    private static Context appContext;
    private DaoSession mDaoSession;
    private MessageBeanDao userBeanDao;

    private DBHelper() {
    }

    //单例模式，DBHelper只初始化一次
    public static DBHelper getInstance(Context context) {
        if (instance == null) {
            instance = new DBHelper();
            if (appContext == null) {
                appContext = context.getApplicationContext();
            }
            instance.mDaoSession = MyApplication.getDaoSession(context);
            instance.userBeanDao = instance.mDaoSession.getMessageBeanDao();
        }
        return instance;
    }


    //删除userBean表
    public void dropSessionTable() {
        userBeanDao.dropTable(mDaoSession.getDatabase(), true);
    }

//    //删除所有表
//    public void dropAllTable() {
//        userBeanDao.dropTable(mDaoSession.getDatabase(), true);
//    }

//    //创建所有表
//    public void createAllTable() {
//        userBeanDao.createTable(mDaoSession.getDatabase(), true);
//    }

    /**
     * insert or update note
     *
     * @param userBean
     * @return insert or update note id
     */
    //插入或者删除session项
    public long saveSession(MessageBean userBean) {
        return userBeanDao.insertOrReplace(userBean);
    }

    public void DeleteSession(MessageBean entity) {
        userBeanDao.delete(entity);
    }

    //获得所有的Session倒序排存到List列表里面
    public List<MessageBean> loadAllMessageBeanReverse() {
        List<MessageBean> sessions = new ArrayList<MessageBean>();
        List<MessageBean> tmpSessions = userBeanDao.loadAll();
        int len = tmpSessions.size();
        for (int i = len - 1; i >= 0; i--) {
            sessions.add(tmpSessions.get(i));
        }
        return sessions;
    }

    public List<MessageBean> searchByAll(String string) {
        List<MessageBean> userBeanList = new ArrayList<MessageBean>();

        userBeanList = userBeanDao.queryBuilder().whereOr(MessageBeanDao.Properties.Id.eq(string), MessageBeanDao
                .Properties.Send_name.like("%"+string+"%"), MessageBeanDao.Properties.Send_id.eq(string),
                MessageBeanDao.Properties.Receive_id.eq(string)).list();

        return userBeanList;
    }

    //删除某一项Session
    public void DeleteNoteBySession(MessageBean entity) {
        QueryBuilder<MessageBean> mqBuilder = userBeanDao.queryBuilder();
        mqBuilder.where(MessageBeanDao.Properties.Id.eq(entity.getId()));
        List<MessageBean> userBeanList = mqBuilder.build().list();
        userBeanDao.deleteInTx(userBeanList);
    }


    //根据id找到某一项
    public MessageBean loadNote(Long id) {
        return userBeanDao.load(id);
    }

    //获得所有的MessageBean列表
    public List<MessageBean> loadAllUserbean() {
        return userBeanDao.loadAll();
    }


    /**
     * query list with where clause
     * ex: begin_date_time >= ? AND end_date_time <= ?
     *
     * @param where  where clause, include 'where' word
     * @param params query parameters
     * @return
     */
    //查询满足params条件的列表
    public List<MessageBean> queryUserbean(String where, String... params) {
        ArrayList<MessageBean> ad = new ArrayList<MessageBean>();
        return userBeanDao.queryRaw(where, params);
    }


    //不一一介绍了，大家可以自己写，有些比较难的查询可以使用QueryBuilder来查询
    public List<MessageBean> loadLastMsgBySessionid(String sessionid) {
        QueryBuilder<MessageBean> mqBuilder = userBeanDao.queryBuilder();
        mqBuilder.where(MessageBeanDao.Properties.Id.eq(sessionid)).orderDesc(MessageBeanDao.Properties.Id).limit
                (1);
        return mqBuilder.list();
    }


    public List<MessageBean> loadMoreMsgById(String sessionid, Long id) {
        QueryBuilder<MessageBean> mqBuilder = userBeanDao.queryBuilder();
        mqBuilder.where(MessageBeanDao.Properties.Id.lt(id)).where(MessageBeanDao.Properties.Id.eq(sessionid))
                .orderDesc(MessageBeanDao.Properties.Id).limit(20);
        return mqBuilder.list();
    }


    /**
     * delete all note
     */
    public void deleteAllNote() {
        userBeanDao.deleteAll();
    }


    /**
     * delete note by id
     *
     * @param id
     */
    public void deleteNote(Long id) {
        userBeanDao.deleteByKey(id);
        Log.i(TAG, "delete");
    }


    public void deleteNote(MessageBean note) {
        userBeanDao.delete(note);
    }
}
